<?php
/*
 PRODUCT_FILTER
 Create & Copyright (C) 2011  xt-shopservice.de
 Sebastian Schramm & Daniel Siekiera & Andreas Kausch
 */

include ('includes/application_top.php');

$smarty = new Smarty;
require (DIR_FS_CATALOG.'templates/'.CURRENT_TEMPLATE.'/source/boxes.php');
$breadcrumb->add(NAVBAR_TITLE_PRODUCT_FILTER, xtc_href_link(FILENAME_PRODUCT_FILTER, '', 'NONSSL'));
require (DIR_WS_INCLUDES.'header.php');

if((isset($_POST)) && (isset($_GET['request']))) {
	if(sizeof($_POST['filter']) > 0) {
		$i = 0;
		foreach($_POST['filter'] AS $term) {
		if($i == 0)
			$in .="'".$term."'";
		else
			$in .=",'".$term."'";
		$i++;
		}
		$_SESSION['filter'] = $in;
		$_SESSION['filter_id'] = $_POST['filter'];
	}

	$having = '';
	if($_POST['verbinder'] == 'true')
		$having = "HAVING CHAR_LENGTH(filter) > ".(strlen(str_replace('\'','',$_SESSION['filter'])) - 1);

	$data_query = xtDBquery(" SELECT GROUP_CONCAT(filter_id) AS filter,products_id FROM products_to_filter WHERE filter_id IN(".$_SESSION['filter'].") GROUP BY products_id ".$having);

	while($data = xtc_db_fetch_array($data_query)){
		if($products_ids != '')
			$products_ids .= ",'".$data['products_id']."'";
		else
			$products_ids = "'".$data['products_id']."'";
	}
	if($products_ids =='') $products_ids = "''";
	       //fsk18 lock
        if ($_SESSION['customers_status']['customers_fsk18_display'] == '0')
	        $fsk_lock = " AND p.products_fsk18 != '1' ";
        else
        	unset ($fsk_lock);


        //group check
        if (GROUP_CHECK == 'true')
        	$group_check = " AND p.group_permission_".$_SESSION['customers_status']['customers_status_id']."=1 ";
        else
        	unset ($group_check);


        //build query
        $select_str = "SELECT DISTINCT
	                  p.products_id, 
	                  p.products_price, 
	                  p.products_model, 
	                  p.products_quantity, 
	                  p.products_shippingtime, 
	                  p.products_fsk18, 
	                  p.products_image, 
	                  p.products_weight, 
	                  p.products_tax_class_id, 
	                  pd.products_name, 
	                  pd.products_short_description, 
	                  pd.products_description ";

        $from_str = "FROM ".TABLE_PRODUCTS." AS p LEFT JOIN ".TABLE_PRODUCTS_DESCRIPTION." AS pd ON (p.products_id = pd.products_id) ";

        $where_str = " WHERE p.products_id  IN(".$products_ids.") AND p.products_status = '1' AND pd.language_id = '".(int)$_SESSION['languages_id']."'".$fsk_lock.$group_check;
		unset($_SESSION['filters']);
		foreach ($_SESSION['filter_id'] AS $id) {
			$name = xtc_db_fetch_array(xtDBquery("SELECT
														id, title
	 												FROM
	 													".TABLE_PRODUCT_FILTER_ITEMS."
	 												WHERE
	 													id = '".$id."'
	 												AND
	 													language_id = '".$_SESSION['languages_id']."'"));
			$_SESSION['filters'][] = $name;
		}
        $smarty->caching = 0;
        $smarty->assign('language', $_SESSION['language']);

        $listing_sql = $select_str.$from_str.$where_str;
        require (DIR_WS_MODULES.'product_filter_listing.php');

} elseif(( isset ($_GET['fcat']) && is_numeric($_GET['fcat'])) && ( isset ($_GET['item']) && is_numeric($_GET['item']))) {

	$module_smarty = new Smarty;
	$module_smarty->assign('tpl_path', 'templates/'.CURRENT_TEMPLATE.'/');

    if ($_SESSION['customers_status']['customers_fsk18_display'] == '0')
        $fsk_lock = " AND p.products_fsk18 != '1' ";
    else
        unset ($fsk_lock);

    if (GROUP_CHECK == 'true')
        $group_check = " AND p.group_permission_".$_SESSION['customers_status']['customers_status_id']."=1 ";
    else
    	unset ($group_check);

    //build query
    $select_str = "SELECT distinct
                  p.products_id,
                  p.products_price,
                  p.products_model,
                  p.products_quantity,
                  p.products_shippingtime,
                  p.products_fsk18,
                  p.products_image,
                  p.products_weight,
                  p.products_tax_class_id,
                  pd.products_name,
                  pd.products_short_description,
                  pd.products_description ";

    $from_str = "FROM
    				products_to_filter p2f, ".TABLE_PRODUCTS." AS p LEFT JOIN ".TABLE_PRODUCTS_DESCRIPTION." AS pd ON (p.products_id = pd.products_id) ";

    $where_str = " WHERE
    					p2f.filter_id = '".(int)$_GET['item']."'
    				AND
    					p.products_id = p2f.products_id
    				AND
    					p.products_status = '1' "."
    				AND
    					pd.language_id = '".(int)$_SESSION['languages_id']."'".$fsk_lock.$group_check;

    $smarty->caching = 0;
    $smarty->assign('language', $_SESSION['language']);

    $select_item_query = xtDBquery("SELECT
    									fi_i.title AS item_title, fi_c.titel AS cat_title
								      FROM
								      	".TABLE_PRODUCT_FILTER_ITEMS." fi_i,
								      	".TABLE_PRODUCT_FILTER_CATEGORIES." fi_c
								      WHERE
								      	fi_i.status = 1
								      AND
								      	fi_c.status = 1
								      AND
								      	fi_i.id = '".(int)$_GET['item']."'
								      AND
								      	fi_i.filter_categories_id = '".(int)$_GET['fcat']."'
								      AND
								      	fi_c.id = fi_i.filter_categories_id
								      AND
								      	fi_i.language_id = '".$_SESSION['languages_id']."'
								      AND
								      	fi_c.language_id = fi_i.language_id");

    $select_item = xtc_db_fetch_array($select_item_query);

    $module_smarty->assign('CATEGORIES_DESCRIPTION', 'Das passende Produkt ist nicht dabei? Versuchen Sie unseren <a href="'.xtc_href_link('product_filter.php', 'advanced_filter='.$_GET['category']).'">erweiterten Filter</a>.');
    $module_smarty->assign('CATEGORIES_HEADING_TITLE', 'Alle treffer f&uuml;r <em>'.$select_item['item_title'].'</em> aus der Kategorie <em>'.$select_item['cat_title'].'</em>');

	$listing_sql = $select_str.$from_str.$where_str;

    if(isset($_GET['per_site']) && !empty($_GET['per_site']))
		$per_site = $_GET['per_site'];
	elseif(isset($_SESSION['per_site']))
		$per_site = $_SESSION['per_site'];
	elseif(!isset($_SESSION['per_site']) || !isset($_GET['per_site']))
		$per_site = MAX_DISPLAY_SEARCH_RESULTS;

	$_SESSION['per_site'] = $per_site;

	if($_GET['view_as_filter'] !='') {
		$list_name = $_GET['view_as_filter'];
		$_SESSION['view_as_filter'] = $_GET['view_as_filter'];
	}
	elseif($_SESSION['view_as_filter'] !='')
		$list_name = $_SESSION['view_as_filter'];

	elseif(!isset($_SESSION['view_as_filter']) || !isset($_GET['view_as_filter'])) {
		$list_name = 'product_filter_list';
		$_SESSION['view_as_filter'] = 'product_filter_list';
	}


	$listing_split = new splitPageResults($listing_sql, (int)$_GET['page'], (int)$_SESSION['per_site'], 'p.products_id');
	$module_content = array ();

	$navigation_smarty = new Smarty;
	$page_links = $listing_split->getLinksArray(MAX_DISPLAY_PAGE_LINKS, xtc_get_all_get_params(array('page', 'info', 'x', 'y', 'cPath', 'cat', 'per_site', 'view_as_filter')), TEXT_DISPLAY_NUMBER_OF_PRODUCTS);

	$navigation_smarty->assign('LINKS', $page_links);
	$navigation_smarty->assign('language', $_SESSION['language']);
	$navigation_smarty->assign('tpl_path', 'templates/'.CURRENT_TEMPLATE.'/');
	$navigation = $navigation_smarty -> fetch(CURRENT_TEMPLATE.'/module/products_page_navigation.html');

	$module_smarty->assign('FILTER_SEARCH', $_SESSION['filters']);

	$listing_query = xtDBquery($listing_split->sql_query);
	$rows = 0;
	while ($listing = xtc_db_fetch_array($listing_query, true)) {
		$rows ++;
		$module_content[] = $product->buildDataArray($listing, 'thumbnail', $list_name, $rows);
	}

	$link = xtc_href_link(FILENAME_PRODUCT_FILTER, xtc_get_all_get_params(array('view_as_filter', 'per_site', 'x', 'y', 'cat', 'cPath')));

	$module_smarty->assign('products_listing_url', $link);

	if($name !='') {
	$module_smarty->assign('filter_request', $namen);
	}
	$module_smarty->assign('language', $_SESSION['language']);
	$module_smarty->assign('module_content', $module_content);
	
	$file_name = FILENAME_PRODUCT_FILTER;

	$options = xtc_db_fetch_array(xtDBquery("SELECT 
													list_name, 
													col, 
													p_img, 
													p_name, 
													p_price, 
													b_details, 
													b_order, 
													b_wishlist, 
													p_reviews, 
													p_stockimg, 
													p_vpe, 
													p_model, 
													p_manu_img, 
													p_manu_name, 
													p_short_desc, 
													p_short_desc_lenght, 
													p_long_desc, 
													p_long_desc_lenght, 
													list_type 
												FROM 
													products_listings 
												WHERE 
													list_name = '".$list_name."' "));

	switch($options['col']) {
		case 2:
			$col_width = 'w474p';
			break;
		case 3:
			$col_width = 'w303p';
			break;
		case 4:
			$col_width = 'w223p';
			break;
		default:
			$col_width = 'w98p';
			break;
	}

	if((int)$count % (int)$options['col'] == 0) $col_class = 'last_li ';
	else $col_class = '';

	if((int)$options['col'] == 1) 
		$col_special_class = 'only_one';
	else 
		$col_special_class = 'gallery_item';
	
	$getCols = xtc_db_fetch_array(xtDBquery("SELECT col FROM products_listings WHERE list_name ='".$list_name."' "));

	if(isset($_GET['page']) && $_GET['page'] !='')
		$page .= '&page='.$_GET['page'];
	
	switch($getCols['col']) {
		case '3' :
			$view_per_site  = ($per_site == 9 ? '<b>9</b>' : '<a href="'.xtc_href_link($file_name, xtc_get_all_get_params(array('products_id', 'x', 'y', 'cat', 'per_site', 'multisort', 'filter_id', 'view_as')).'per_site=9'.$get_param).'">9</a>').' | ';
			$view_per_site .= ($per_site == 18 ? '<b>18</b>' : '<a href="'.xtc_href_link($file_name, xtc_get_all_get_params(array('products_id', 'x', 'y', 'cat', 'per_site', 'multisort', 'filter_id', 'view_as')).'per_site=18'.$get_param).'">18</a>').' | ';
			$view_per_site .= ($per_site == 27 ? '<b>27</b>' : '<a href="'.xtc_href_link($file_name, xtc_get_all_get_params(array('products_id', 'x', 'y', 'cat', 'per_site', 'multisort', 'filter_id', 'view_as')).'per_site=27'.$get_param).'">27</a>').' | ';
			$view_per_site .= ($per_site == 45 ? '<b>45</b>' : '<a href="'.xtc_href_link($file_name, xtc_get_all_get_params(array('products_id', 'x', 'y', 'cat', 'per_site', 'multisort', 'filter_id', 'view_as')).'per_site=45'.$get_param).'">45</a>').' | ';
			$view_per_site .= ($per_site == 81 ? '<b>81</b>' : '<a href="'.xtc_href_link($file_name, xtc_get_all_get_params(array('products_id', 'x', 'y', 'cat', 'per_site', 'multisort', 'filter_id', 'view_as')).'per_site=81'.$get_param).'">81</a>');
			break;

		case '4' :
			$view_per_site  = ($per_site == 12 ? '<b>12</b>' : '<a href="'.xtc_href_link($file_name, xtc_get_all_get_params(array('products_id', 'x', 'y', 'cat', 'per_site', 'multisort', 'filter_id', 'view_as')).'per_site=12'.$get_param).'">12</a>').' | ';
			$view_per_site .= ($per_site == 24 ? '<b>24</b>' : '<a href="'.xtc_href_link($file_name, xtc_get_all_get_params(array('products_id', 'x', 'y', 'cat', 'per_site', 'multisort', 'filter_id', 'view_as')).'per_site=24'.$get_param).'">24</a>').' | ';
			$view_per_site .= ($per_site == 60 ? '<b>60</b>' : '<a href="'.xtc_href_link($file_name, xtc_get_all_get_params(array('products_id', 'x', 'y', 'cat', 'per_site', 'multisort', 'filter_id', 'view_as')).'per_site=60'.$get_param).'">60</a>').' | ';
			$view_per_site .= ($per_site == 84 ? '<b>84</b>' : '<a href="'.xtc_href_link($file_name, xtc_get_all_get_params(array('products_id', 'x', 'y', 'cat', 'per_site', 'multisort', 'filter_id', 'view_as')).'per_site=84'.$get_param).'">84</a>').' | ';
			$view_per_site .= ($per_site == 96 ? '<b>96</b>' : '<a href="'.xtc_href_link($file_name, xtc_get_all_get_params(array('products_id', 'x', 'y', 'cat', 'per_site', 'multisort', 'filter_id', 'view_as')).'per_site=96'.$get_param).'">96</a>');
			break;

		default :
			$view_per_site  = ($per_site == 10 ? '<b>10</b>' : '<a href="'.xtc_href_link($file_name, xtc_get_all_get_params(array('products_id', 'x', 'y', 'cat', 'per_site', 'multisort', 'filter_id', 'view_as')).'per_site=10'.$get_param).$page.'">10</a>').' | ';
			$view_per_site .= ($per_site == 20 ? '<b>20</b>' : '<a href="'.xtc_href_link($file_name,  xtc_get_all_get_params(array('products_id', 'x', 'y', 'cat', 'per_site', 'multisort', 'filter_id', 'view_as')).'per_site=20'.$get_param).$page.'">20</a>').' | ';
			$view_per_site .= ($per_site == 30 ? '<b>30</b>' : '<a href="'.xtc_href_link($file_name, xtc_get_all_get_params(array('products_id', 'x', 'y', 'cat', 'per_site', 'multisort', 'filter_id', 'view_as')).'per_site=30'.$get_param).$page.'">30</a>').' | ';
			$view_per_site .= ($per_site == 50 ? '<b>50</b>' : '<a href="'.xtc_href_link($file_name, xtc_get_all_get_params(array('products_id', 'x', 'y', 'cat', 'per_site', 'multisort', 'filter_id', 'view_as')).'per_site=50'.$get_param).$page.'">50</a>').' | ';
			$view_per_site .= ($per_site == 100 ? '<b>100</b>' : '<a href="'.xtc_href_link($file_name, xtc_get_all_get_params(array('products_id', 'x', 'y', 'cat', 'per_site', 'multisort', 'filter_id', 'view_as')).'per_site=100'.$get_param).$page.'">100</a>');
			break;
	}

	$per_site_html = new Smarty;
	$per_site_html->assign('LINKS_PER_SITE', $view_per_site);
	$per_site_html->assign('language', $_SESSION['language']);
	$products_persite = $per_site_html->fetch(CURRENT_TEMPLATE.'/module/products_per_site.html');	
	
	switch($list_name) {
		case 'product_filter_list' :
			$views_as = '<a href="'.xtc_href_link($file_name, xtc_get_all_get_params(array('products_id', 'x', 'y', 'cat', 'per_site', 'multisort', 'filter_id', 'view_as_filter')).'view_as_filter=product_filter_grid'.$get_param).$page.'">'. LISTING_GALLERY .'</a> '. LISTING_LIST_ACTIVE;
			break;
		default :
			$views_as = LISTING_GALLERY_ACTIVE .' <a href="'.xtc_href_link($file_name, xtc_get_all_get_params(array('products_id', 'x', 'y', 'cat', 'per_site', 'multisort', 'filter_id', 'view_as_filter')).'view_as_filter=product_filter_list'.$get_param).$page.'">'. LISTING_LIST .'</a>';
			break;
	}

	$view = new Smarty;
	$view->assign('LINKS_VIEW_AS', $views_as);
	$view->assign('language', $_SESSION['language']);
	$views = $view->fetch(CURRENT_TEMPLATE.'/module/products_view_as.html');
	$module_smarty->assign('PRODUCTS_VIEW_AS', $views);
	$module_smarty->assign('PRODUCTS_PER_SITE', $products_persite);
	$module_smarty->assign('NAVIGATION', $navigation);
	$module_smarty->assign('product_filter', true);
	$module_smarty->assign('CLASS', 'product_filter');

	$module_smarty->caching = 0;
	$main_content = $module_smarty->fetch(CURRENT_TEMPLATE.'/module/product_listings.html');

	$smarty->assign('main_content', $main_content);

	$smarty->assign('language', $_SESSION['language']);

	if(!defined(RM))
	$smarty->load_filter('output', 'note');
	$smarty->display(CURRENT_TEMPLATE.'/index.html');

} elseif (isset($_GET['advanced_filter']) || !isset ($_GET['fcat'])) {
        $filter_array = array ();
        $cat_count = xtDBquery("SELECT id, titel, categories_ids FROM ".TABLE_PRODUCT_FILTER_CATEGORIES." WHERE language_id = '".$_SESSION['languages_id']."' AND status = '1'");
        $count = xtc_db_num_rows($cat_count);

        while ($cat_data = xtc_db_fetch_array($cat_count)) {
        	$checkID = explode('|', $cat_data['categories_ids']);
        	if(in_array($_GET['advanced_filter'], $checkID) || $cat_data['categories_ids'] == 'all') {
				$filter_array[$cats] = array(	'CATEGORIE_ID'     => $cat_data['id'],
												'CATEGORIE_TITLE'  => $cat_data['titel'],
												'ITEMS'            => '');

	            $filter_query = xtDBquery("SELECT
	            									id, name, filter_categories_id, position
		            							FROM
		            								".TABLE_PRODUCT_FILTER_ITEMS."
		            							WHERE
		            								filter_categories_id = '".$cat_data['id']."'
		            							AND
		            								language_id = '".$_SESSION['languages_id']."'
		            							AND
		            								status = '1'
		            							ORDER BY
		            								position ASC");

	            while ($filter = xtc_db_fetch_array($filter_query)) {
			    	$filter_array[$cats]['ITEMS'][$item] = array ('id'=>$filter['id'],
				    											'text'=>$filter['name'],
				    											'checkbox_id' => $filter['id'],
				    											'checkbox_checked' => (in_array($filter['id'], $_POST['filter']) ? true : false));
			    	$item++;
			    }
			    $cats++;
        	}
        }

		$smarty->assign('VERBINDER', '<input name="verbinder" type="radio" value="true" checked="" /> '. PRODUCT_FILTER_AND . '<br /><input name="verbinder" type="radio" value="false" /> '. PRODUCT_FILTER_OR."\n".xtc_draw_hidden_field('cat', $_GET['advanced_filter']));
		$smarty->assign('filter_search_items', $filter_array);
		$smarty->assign('FORM_START',xtc_draw_form('advanced_filter_search',xtc_href_link('product_filter.php', 'request', 'NONSSL', false),'post').xtc_hide_session_id());
		$smarty->assign('BUTTON_SUBMIT', xtc_image_submit('button_search.gif', IMAGE_BUTTON_SEARCH));
		$smarty->assign('FORM_END','</form>');

		$main_content = $smarty->fetch(CURRENT_TEMPLATE.'/module/product_filter.html');
		$smarty->assign('main_content', $main_content);

		$smarty->assign('language', $_SESSION['language']);

		if(!defined(RM))
			$smarty->load_filter('output', 'note');
		$smarty->display(CURRENT_TEMPLATE.'/index.html');

} 
include ('includes/application_bottom.php');
?>